﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace bci2.socket
{
    public class BCIMessage
    {
                /*This file is part of AENIMA. AENIMA is a module of BCI++ framework for the implementation of brain computerinterface systems. 
        AEnima is intended for use with BCI++ only.

        BCI++ is a project of the Sensibilab of the Politecnico di Milano. 

        AENIMA is free software: you can redistribute it and/or modify
        it under the terms of the GNU Lesser General Public License as 
        published by the Free Software Foundation, either version 3 of 
        the License, or (at your option) any later version.

        AENIMA is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU Lesser General Public License for more details.

        You should have received a copy of the GNU General Public License
        and the GNU Lesser General Public License along with AENIMA.  
        If not, see <http://www.gnu.org/licenses/>.*/

        /*!
            \file	BCIMessage.h
             This struct is the message that the Hardware Interface Module and AEnima use to communicate on TCP/IP socket. 

            \author	Sensibilab EMail: info.sensibilab@polimi.it

            \date	2007-09-06 creation \n
	            2009-01-27 last modification

            \b COPYRIGHT: 
	            This program is property of Sensibilab - Campus Ponit - Lecco
        */


        //Instrument control
        public const int	START_ACQ				=	101;
        public const int	STOP_ACQ				=	102;
        public const int CONNECTION_COMPLETED = 103;

        //Triggers event
        public const int GRAPH_TRIG = 201;

        //Messages from algorithm to AEnima
        public const int CLASSIFICATION = 301;
        public const int FEEDBACK = 302;
        public const int SET_PARAMS = 303;

        //Messagese from AEnima to algorithm
        public const int RESET_ALGORITHM = 401;
        public const int REQUEST_PARAMS = 402;

        //Generic messages
        public const int BCI_OK = 501;
        public const int BCI_ERROR = 502;

        /*!\struct BCIMessage
        \param kind Message type:\nSTART_ACQ, STOP_ACQ, CONNECTION_COMPLETED, GRAPH_TRIG, CLASSIFICATION, FEEDBACK, SET_PARAMS, RESET_ALGORITHM, REQUEST_PARAMS, BCI_OK, BCI_ERROR.\n
        For detail see AEnima overview.doc.
        \param value In some case it can be usefull to send and receive a value during the communication.
        \param Buffer The buffer can carry up to 16 int value.
        \param Check This value is used to control if the sent/received message is correct.*/
        public struct BCIMes
        {
            public int Kind;//Used to define which message you are sending
            public int Value;//Used for single value messages
            public int[] Buffer;//Storage space for DATA
            public int Check; //MS Kind+Value;
        }
        
        public BCIMes message;

        /*!\fn BCIMessage ComposeMessage(int Kind, int Value, int *pBuffer,int BuffSize)
        This function can be used to compose a BCIMessage*/
        public int Value = 0;
        public int[] pBuffer = new int[16];
        public int BuffSize=0;
        
        public BCIMes ComposeMessage(int Kind, int Value, int[] pBuffer,int BuffSize)
        {
	        BCIMes Ret = new BCIMes();
            Ret.Buffer = new int[16];
            for (int i = 0; i < Ret.Buffer.Length; i++ )
                Ret.Buffer[i] = 0;

	        Ret.Kind = Kind;
	        Ret.Value = Value;
	        Ret.Check = Kind+Value;
	        if(pBuffer.Length != 0 && BuffSize != 0)
                Ret.Buffer = pBuffer;

	        return Ret;
        }

        public BCIMessage() { }
    }
}
